<?php
	namespace Ensao\AppBundle\Controller;
	use Symfony\Bundle\FrameworkBundle\Controller\Controller;
	use Symfony\Component\HttpFoundation\Response;
	use Ensao\AppBundle\Entity\Evenement;
    use Ensao\AppBundle\Entity\TypeEvenement;
    use Ensao\AppBundle\Entity\Dossier;

class EvenementController extends Controller
{
    public function indexAction()
    {
    	$em = $this->getDoctrine()->getEntityManager();
    	$listes = $em->getRepository('EnsaoAppBundle:Evenement')->findAll();
        $listeDossier = $em->getRepository('EnsaoAppBundle:Dossier')->findAll();
        return $this->render('EnsaoAppBundle:Evenement:index.html.twig',array('listes'=>$listes,'listeDossier'=>$listeDossier));
    }

    public function ajouterAction()
    {
        if( $this->get('request')->getMethod() == 'POST' )
        {
            $em = $this->getDoctrine()->getEntityManager(); // ORM
            $req=$this->get('request'); // recuperer l'obj request
            // Declaration des objs
            $event=new Evenement();
            $type_event=new TypeEvenement();
            $dossier=new Dossier();
            // Initialisation de l'obj
            $event->setLibelle($req->request->get('event_libelle'));
            $event->setDescription($req->request->get('event_desc'));
            $event_date = new \DateTime($req->request->get('event_date')." ".$req->request->get('event_heure'));
            $event->setDate($event_date);
            if($req->request->get('event_type') != 5) // Si le type de l'event est une audiance ou une procedure
            {
                if($req->request->get('event_typeDossier') == 'on')
                {
                    // Dossier Elec
                    $dossier=$em->getRepository('EnsaoAppBundle:Dossier')->find($req->request->get('event_numDossierElec'));
                    $event->setDossier($dossier);
                }
                else
                {
                    $event->setNumDossierSimple($req->request->get('event_numDossierSimple'));
                }
            }
            // recuperer le type de l'event
            $type_event=$em->getRepository('EnsaoAppBundle:TypeEvenement')->find($req->request->get('event_type'));
            $event->setTypeEvenement($type_event);
            // persister l'obj
            $em->persist($event);
            $em->flush();
            // definir un message
            $this->get('session')->setFlash("notification","تمت العملية بنجاح");
        }
         return $this->redirect($this->generateUrl('evenement_accueil'));
    }
    public function eventDropAction()
    {
        $req=$this->get('request');
        if($req->isXmlHttpRequest())
        {
            $em = $this->getDoctrine()->getEntityManager(); // ORM
            $event=$em->getRepository('EnsaoAppBundle:Evenement')->find($req->request->get('id'));
            $nouveauDate=new \DateTime();
            if($req->request->get('nbrJour') > 0) // valeur positive
                $nouveauDate=date_add($event->getDate(),new \DateInterval('P'.$req->request->get('nbrJour').'D'));
            else
                $nouveauDate=date_sub($event->getDate(),new \DateInterval('P'.($req->request->get('nbrJour')*-1).'D'));
            $event->setDate(new \DateTime($nouveauDate->format('Y-m-d')));
            $em->flush();
            return new Response('<body>success</body>');
        }
        // proteger cette methode a l'acces direct ! (execption)
    }
    public function afficherAction(){
        $req=$this->get('request');
        if($req->isXmlHttpRequest())
        {
            $em = $this->getDoctrine()->getEntityManager(); // ORM
            $event=$em->getRepository('EnsaoAppBundle:Evenement')->find($req->request->get('id'));
            $eventJson=json_encode(array('id' => $event->getId(),
                                        'libelle' => $event->getLibelle(),
                                        'date' => $event->getDate(),
                                        'description'=> $event->getDescription(),
                                        'numDossierSimple' => $event->getNumDossierSimple(),
                                        'numDossier' => $event->getDossier()->getNumDossier(),
                                        'libelleTypeEvenemt' =>  $event->getTypeEvenement()->getLibelle()
                                     ));//jscon encode the array
            return new Response($eventJson,200,array('Content-Type'=>'application/json'));//make sure it has the correct content type

        }
        // proteger cette methode a l'acces direct ! (execption)
    }
}